package com.test.javafaq_search.kafka;

import com.google.gson.Gson;
import com.test.javafaq_commons.vo.Topic;
import com.test.javafaq_search.service.IQuestionService;
import com.test.javafaq_search.vo.QuestionVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class KafkaConsumer {
    private Gson gson = new Gson();

    @Autowired
    IQuestionService questionService;

    @KafkaListener(topics = Topic.QUESTIONS)
    public void reciveQuestion(ConsumerRecord<String,String> record){
        String json = record.value();
        log.debug("接收到：{}",json);
        QuestionVo questionVo = gson.fromJson(json, QuestionVo.class);
        questionService.saveQuestion(questionVo);
        log.debug("新增完成question从mysql到elascitsearch...");
    }

}
